﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	internal class LayFanJinDrawUtility
	{
		public static List<Line> DrawFlowTo(Autodesk.Revit.DB.View view, XYZ startPoint, XYZ currentPoint, Line line, ref bool rotate, ref XYZ verticalVectore, ref XYZ layPoint)
		{
			List<Line> list = new List<Line>();
			try
			{
				double num = 1.4763779527559053 * (double)view.Scale / 100.0;
				double num2 = 3.9370078740157481 * (double)view.Scale / 100.0;
				XYZ verticalVector = LayFanJinDrawUtility.GetVerticalVector(ref line);
				line.MakeUnbound();
				XYZ xyzpoint = line.Project(startPoint).XYZPoint;
				XYZ xyz = line.Project(currentPoint).XYZPoint.Subtract(xyzpoint).Normalize();
				if (xyz.AngleTo(view.RightDirection) > 1.5717963267948964)
				{
					rotate = true;
				}
				else if (xyz.AngleTo(view.RightDirection) <= 1.5717963267948964 && xyz.AngleTo(view.RightDirection) >= 1.5697963267948967)
				{
					if (xyz.AngleTo(view.UpDirection) > Math.PI*.5)
					{
						rotate = false;
					}
					else
					{
						rotate = true;
					}
				}
				else
				{
					rotate = false;
				}
				verticalVectore = verticalVector;
				layPoint = xyzpoint;
				XYZ startPoint2 = xyzpoint;
				XYZ xyz2 = xyzpoint.Add(xyz * (num2 + num * Math.Sqrt(3.0) / 2.0));
				XYZ endPoint = xyz2.Subtract(xyz * num * Math.Sqrt(3.0) / 2.0).Add(verticalVector * num / 2.0);
				XYZ endPoint2 = xyz2.Subtract(xyz * num * Math.Sqrt(3.0) / 2.0).Add(-verticalVector * num / 2.0);
				list.Add(YJKLineEx.YJKGetBound(startPoint2, xyz2));
				list.Add(YJKLineEx.YJKGetBound(xyz2, endPoint));
				list.Add(YJKLineEx.YJKGetBound(xyz2, endPoint2));
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		private static XYZ GetVerticalVector(ref Line line)
		{
			XYZ xyz = new XYZ(line.GetEndPoint(0).X, line.GetEndPoint(0).Y, 0.0);
			XYZ xyz2 = new XYZ(line.GetEndPoint(1).X, line.GetEndPoint(1).Y, 0.0);
			XYZ xyz3 = xyz2.Subtract(xyz).Normalize();
			line = Line.CreateBound(xyz, xyz2);
			return new XYZ(-xyz3.Y, xyz3.X, 0.0);
		}
	}
}
